#!/usr/bin/env node

const cheerio = require('cheerio'),
      log = console.log,
      http = require('http'),
      https = require('https');
let imgArr = [];
https.get('https://ke.segmentfault.com/free',(res) => {
      let result = '';
      res.on('data',(data) => {
        result += data.toString('utf8');
      })
      res.on('end',()=>{ 
          let $ = cheerio.load(result);
          $('body').find('.card-title>a').each(function(){
            let cName = $(this).text(),
              cURL = 'https://ke.segementfault.com' + $(this).attr('href');
            if(cName === '') return;
            log('课程名称',cName);
            log('课程网址',cURL.trim());
            log('');
          })
          $('body').find('.rounded-top>img').each(function(){
            let imgName = $(this).attr('src');
            imgArr.push(imgName);
          })
      })
})
http.createServer((req,res) => {
  for(let i = 0;i < imgArr.length;i++){
    res.write(`<html><head>图片</head><body><img src='${imgArr[i]}'/></body></html>`);
  }
}).listen(3000);

